package com.ch07.service;

import com.ch07.mapper.CityMapper;
import com.ch07.pojo.City;
import com.ch07.utils.MyBatisSqlSessionFactory;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @ClassName CityService
 * @Description TODO
 * @Author Kx
 * @Date 2025/4/25 8:49
 * @Version 1.0
 */
public class CityService {
    /**
     * @author: kx
     * @date: 2025/4/25 9:36
     * @param pageNum : 当前页码
     * @param pageSize : 每页显示的记录数
     * @return java.util.List<com.ch07.pojo.City>
     * @description: 使用mybatis自带的RowBound对象进行分页
    */
    public List<City> pageCity(int pageNum, int pageSize) {
        // 将分页参数封装成RowBounds对象
        RowBounds rowBounds = new RowBounds(pageNum, pageSize);

        // 获取SqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            // 获取Mapper代理对象并调用方法
            return sqlSession.getMapper(CityMapper.class).pageCity(rowBounds);
        }
    }

    /** 
     * @author: kx
     * @date: 2025/4/25 9:38
     * @param pageNum : 当前页码
     * @param pageSize : 每页显示的记录数
     * @return java.util.List<com.ch07.pojo.City>
     * @description: 使用插件进行分页查询
    */
    public PageInfo<City> pageCity2(int pageNum, int pageSize) {
        // 获取SqlSession对象
        try (SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSession()) {
            // 获取Mapper代理对象并调用方法
            List<City> cities = sqlSession.getMapper(CityMapper.class).pageCity2(pageNum, pageSize);

            // 将分页记录封装成分页对象(总记录数、总页数、起始页、下一页)
            return new PageInfo<>(cities);
        }
    }
}
